package com.thkj.common.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;

public class RequestUtil {
	
    //json转map
	public static Map<String, Object> getParameterMap(HttpServletRequest request) {
		Map<String, Object> parameter = new HashMap<String, Object>();
		Map params = request.getParameterMap();
		for (Iterator iter = params.keySet().iterator(); iter.hasNext();) {
			String name = (String) iter.next();
			String[] values = (String[]) params.get(name);
			String valueStr = "";
			for (int i = 0; i < values.length; i++) {
				valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
			}
			if (!StringUtils.isBlank(valueStr)) {
				parameter.put(name, valueStr);
			}
		}
		return parameter;
	}
	
	public static String getIp(HttpServletRequest request){
        String ip = request.getHeader("x-forwarded-for");
        request.getHeaderNames();
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
}
